package com.koron.car.web.authorized.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.car.web.authorized.bean.entity.CarReplace;
import com.koron.car.web.authorized.bean.query.ReplaceDelimitQuery;
import com.koron.car.web.authorized.bean.vo.CarReplaceVO;
import com.koron.car.web.authorized.bean.vo.ReplaceDelimitVO;
import com.koron.car.web.authorized.filter.CarReplaceFilter;
import com.koron.car.web.authorized.service.CarReplaceService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/delimit/replace")
@Api(tags = "车辆编制-车辆更购")
public class CarReplaceController implements WorkflowController {

    @Autowired
    private CarReplaceFilter filter;

    @Autowired
    private CarReplaceService replaceService;

    @GetMapping("/selectOne")
    @ApiOperation("根据id查询信息")
    @ApiImplicitParam(name = "id", value = "概况id", required = true)
    public Response<CarReplace> selectOne(@RequestParam String id) {
        log.info("根据id查询信息， id = {}", id);
        CarReplace bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "queryById", CarReplace.class, id);
        return Response.ok(bean);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或更新更购信息")
    public Response<CarReplaceVO> saveOrUpdate(@RequestBody CarReplaceVO replace) {
        log.info("新增或更新更购信息， replace = {}", JSON.toJSONString(replace));
        CarReplaceVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "saveOrUpdate", CarReplaceVO.class, replace);
        return Response.ok(bean);
    }

    @GetMapping("/queryList")
    @ApiOperation("分页查询")
    public Response<PageInfo<ReplaceDelimitVO>> queryList(ReplaceDelimitQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        log.info("分页查询， query = {}", JSON.toJSONString(query));
        List<ReplaceDelimitVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除更购信息")
    public Response<Boolean> delete(@RequestParam String id) {
        log.info("根据id删除更购信息， id = {}", id);
        Boolean flag = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "deleteById", Boolean.class, id);
        return Response.ok(flag);
    }


    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        CarReplaceVO bean = JSON.parseObject(redirectQuery.getFormVariables(), CarReplaceVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), replaceService, "auditFinish", CarReplaceVO.class, bean);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("车辆更购审批设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("车辆更购审批选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }
}
